package demo4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-05-09
 * Time: 21:09
 */

/**
 * 创建对象的方式
 */


class Person implements Cloneable{
    public int age;
    public void eat(){
        System.out.println("吃");
    }
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {return super.clone();} //没有具体的方法 但是也必须要重写
}


public class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.age = 99;
        Person person1 = (Person) person.clone();// 而且还会抛异常
        //一个对象要克隆产生一个副本 那么这个引用所引用的对象一定是可克隆的
        //所有要给这个类是实现clone接口
        System.out.println(person1);
        System.out.println("=================");
        person1.age=10;
        System.out.println(person);
        System.out.println(person1);
    }
}
